// pages/u_Rorder/u_Rorder.js
const app = getApp()
var get_my_order;var page = 1;var Pay;var save_order_pay;var save_order_qj;var save_order_usd;var my_yx_coupons;var save_coupons_pay;var reduction_order;

// 请求数据
var loadMore = function (that) {
  that.setData({
    hidden: false,
    stopdowning: true
  });
  wx.request({
    url: get_my_order,
    data: {
      page: page,
      uid:that.data.aid,
      type:that.data.type
    },
    success: function (res) {
      console.log(res.data.list);
      var list = res.data.list;
      var goods = that.data.goods;
      if (list.length != 0) {
        for (var i = 0; i < list.length; i++) {
          goods.push(list[i]);
        }
      }
      that.setData({
        goods: goods,
        stopdowning: false
      });
      page++;
      if (goods.length == 0) {
        that.setData({
          hidden: true,
          stopdowning: true
        });
      } else {
        that.setData({
          hidden: false
        });
      }
    }
  });
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    head:[
      {name:'全部',type:''},
      {name:'竞价中',type:1},
      {name:'待付款',type:2},
      {name:'待取件',type:3},
      {name:'运输中',type:4},
      {name:'待确认',type:5},
      {name:'待评价',type:6}
    ],
    head_index:0,
    goods: [],
    stopdowning: false,
    hidden: false,
    aid:'',
    type:'',
    yhq:[],
    fix:false,
    o_id:'',
    price:'',
  },
  hidetap:function(e){
    this.setData({
      fix:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    var aid = wx.getStorageSync('uid');
    get_my_order = app.data.ucenter_url + 'get_my_order';
    Pay = app.data.ucenter_url + 'Pay';
    save_order_pay = app.data.ucenter_url + 'save_order_pay';
    save_order_qj = app.data.ucenter_url + 'save_order_qj';
    save_order_usd = app.data.ucenter_url + 'save_order_usd';
    my_yx_coupons = app.data.ucenter_url + 'my_yx_coupons';
    save_coupons_pay = app.data.ucenter_url + 'save_coupons_pay';
    reduction_order = app.data.ucenter_url + 'reduction_order';
    if(type == ''){
      that.setData({
        head_index:0
      })
    }else{
      that.setData({
        head_index:type
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight)
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    }) 
    var goods = [];page = 1;
    that.setData({
      goods: goods,
      stopdowning: false,
      aid:aid,
      type:type,
    })
    if (!that.data.stopdowning) {
      loadMore(that);
    }

    wx.request({
      url: my_yx_coupons,
      data: {
        uid:aid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data);
        var yhq = res.data.list;
        for (var i = 0; i < yhq.length; i++) {
          yhq[i].info.amount = Number(yhq[i].info.amount).toFixed(0);
          yhq[i].info.full = Number(yhq[i].info.full).toFixed(0);
        }
        that.setData({
          yhq:yhq
        })
      }
    })
  },
  headtap:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    var goods = [];page = 1;
    that.setData({
      goods: goods,
      stopdowning: false,
      type:type,
      head_index:index
    })
    if (!that.data.stopdowning) {
      loadMore(that);
    }
  },
  xqtap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/i_detail/i_detail?id='+id,
    })
    // wx.navigateTo({
    //   url: '/pages/u_orderxq/u_orderxq?id='+id
    // })
  },
  paytap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
    var yhq = that.data.yhq;
    if(yhq.length != 0){
      that.setData({
        fix:true,
        o_id:id,
        price:price
      })
    }else{
      that.all_pay(id,1);
    }
  },
  zjtap:function(e){
    var that = this;
    var o_id = that.data.o_id;
    that.all_pay(o_id,1);
    that.setData({
      fix:false
    })
  },
  sytap:function(e){
    var that = this;
    var o_id = that.data.o_id;
    var price = that.data.price;
    var yhq_id = e.currentTarget.dataset.yhq_id;
    var amount = e.currentTarget.dataset.amount;
    var full = e.currentTarget.dataset.full;
    if(Number(price) < Number(full)){
      wx.showModal({
        title: '提示',
        content: '未满足优惠券使用金额！',
        showCancel: false
      })
    }else{
      var pay = (Number(price)-Number(amount)).toFixed(2);
      wx.request({
        url: save_coupons_pay,
        data: {
          oid:o_id,
          coupons_id:yhq_id,
          pay:pay,
          yh_price:amount
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log(res.data);
          that.setData({
            fix:false
          })
          if(res.data.state == 1){
            that.all_pay(o_id,2);
          }
        }
      })
    }
  },
  all_pay:function(id,type){
    var that = this;
    var aid = wx.getStorageSync('uid');
    var openid = wx.getStorageSync('user');
    wx.showLoading();
    wx.request({
      url: Pay,
      data: {
        id: id,
        openid: openid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            console.log(res);
            wx.request({
              url: save_order_pay,
              data: {
                id: id
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
                console.log(res.data)
                wx.hideLoading();
                wx.showModal({
                  title: '提示',
                  content: '支付成功！',
                  showCancel: false,
                  success: function () {
                    var goods = [];page = 1;
                    that.setData({
                      goods: goods,
                      stopdowning: false
                    })
                    if (!that.data.stopdowning) {
                      loadMore(that);
                    }
                  }
                })
              }
            })
          },
          'fail': function (res) {
            console.log(res);
            wx.hideLoading();
            wx.showModal({
              title: '提示',
              content: '支付失败',
              showCancel: false
            })
            if(type == 2){
              wx.request({
                url: reduction_order,
                data: {
                  oid:id
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success (res) {
                  console.log(res.data);
                }
              })
            }
          }
        })
      }
    })
  },
  qutap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否确认已取件',
      showCancel: true,
      success:function(e){
        if(e.confirm){
          wx.request({
            url: save_order_qj,
            data: {
              id:id
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
              console.log(res.data);
              if(res.data.state == 1){
                wx.showModal({
                  title: '提示',
                  content: '操作成功！',
                  showCancel: false,
                  success:function(e){
                    var goods = [];page = 1;
                    that.setData({
                      goods: goods,
                      stopdowning: false
                    })
                    if (!that.data.stopdowning) {
                      loadMore(that);
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  shoutap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否确认已送达',
      showCancel: true,
      success:function(e){
        if(e.confirm){
          wx.request({
            url: save_order_usd,
            data: {
              id:id
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
              console.log(res.data);
              if(res.data.state == 1){
                wx.showModal({
                  title: '提示',
                  content: '操作成功！',
                  showCancel: false,
                  success:function(e){
                    var goods = [];page = 1;
                    that.setData({
                      goods: goods,
                      stopdowning: false
                    })
                    if (!that.data.stopdowning) {
                      loadMore(that);
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  pjtap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/u_fpj/u_fpj?id='+id,
    })
  },
  bindDownLoad: function () {
    var that = this;
    console.log(that.data.stopdowning);
    if (!that.data.stopdowning) {
      loadMore(that);
    } else {
      console.log('gg');
      return;
    }
  },
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})